import java.util.Arrays;

public class lengthOfLongestSubstring {
    public static void main(String[] args) {
        System.out.println(lengthOfLongestSubstring("abcabcbb"));
    }
    public static int lengthOfLongestSubstring(String s) {
        char[] arr = s.toCharArray();
        int[] Hash = new int[128];
        int left = 0, right = 0,n = s.length();
        int ret = 0;
        while (right < n) {
            Hash[arr[right]]++;
            while(Hash[arr[right]] > 1) {
                Hash[arr[left]]--;
                left++;
            }
                ret = Math.max(ret,right-left+1);
                right++;
            }

        return ret;
    }
        public static int lengthOfLongestSubstring1(String s) {
        int[] Hash = new int[126];
        char[] str = s.toCharArray();
         int count = 0,j = 0,i = 0;
            Arrays.fill(Hash,-1);
            for (; i < s.length(); i++) {
                if(Hash[str[i]-' '] != -1) {
                   count = Math.max(i-j , count);
                   if (j < Hash[str[i]-' '] + 1)j = Hash[str[i]-' '] +1;
            }
                Hash[str[i] - ' ']  = i;
        }
            if(Hash[str[i-1]-' '] != -1) {

                count = Math.max(i-j , count);
            }

           return count;

    }
}
